'use strict'

var http = require('http');
var cheerio = require('cheerio');
var _ = require('underscore');
var topics = [],users;

var options = {
    cnNode: {
        host: 'cnodejs.org',
        port: 80,
        path: '/',
        headers: {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36'
        }
    }
}

function getHtml(options,callback) {
    var html;
    http.get(options, function (res) {
        res.on('data', function (data) {
            html += data;
        });
        res.on('end', function () {
            callback(html);
        });
    })
}

function parseSummary(html) {
    var $ = cheerio.load(html);
    $('#topic_list').find('div.cell').each(function () {
        var $item = $(this), $content;
        $content = $item.find('a.topic_title');
        var entity = {
            user: $item.find('.user_avatar').attr('href').replace('/user/',''),
            visits: $item.find('.count_of_visits').text().trim(),
            replies: $item.find('.count_of_replies').text().trim(),
            title: $content.attr('title'),
            id: $content.attr('href').replace('/topic/','')
        };
        getContent(entity);
        topics.push(entity);
    });
}

function getSummaries() {
    var optSummaries = _.clone(options.cnNode);
    for(var idx=1;idx<10;idx++){
        optSummaries.path = '/?tab=all&page='+idx;
        getHtml(optSummaries,parseSummary);
    }
}

function getContent(entity) {
    var opts = _.clone(options.cnNode);
    opts.path = '/topic/'+entity.id;
    getHtml(opts,function (html) {
        var $ = cheerio.load(html),
            $content = $('.topic_content');

        entity.content = $content.text();
    })
}

getSummaries();